<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;

class MovieController extends Controller
{
    //发现主页
    public function index()
    {

        // 查询遍历全球新片
        // 查询id最大的电影
        $firstMovie = DB::table('types') -> orderBy('id', 'desc') -> first();
        $name = explode(',', $firstMovie -> name)[0];
        $firstMovie -> name = $name;

        //获取最大id
        $bigId = $firstMovie -> id;
        
        // 查询id小于最大电影id的4个电影
        $fourMovie = DB::table('types')
            -> where('id', '<', $bigId)
            -> orderBy('id', 'desc')
            -> limit('4')
            -> get();
        //获取4个电影的中文名
        $fourAll = array();
        $i = 0;
        foreach($fourMovie as $four)
        {
            $fourName = explode(',', $four -> name)[0];
            $fourAll[] = $fourName;
            $fourMovie[$i] -> name = $fourAll;
            $i++;
        }


        //获取4个电影中最小id
        $smallId = $fourMovie[3] -> id;

        // 查询id小于4个电影最小id的7个电影
        $sevenMovie = DB::table('types')
            -> where('id', '<', $smallId)
            -> orderBy('id', 'desc')
            -> limit('7')
            -> get();

        //获取7个电影的中文名
        $sevenAll = array();
        $i = 0;
        foreach($sevenMovie as $seven)
        {
            $sevenName = explode(',', $seven -> name)[0];
            $sevenAll[] = $sevenName;
            $sevenMovie[$i] -> name = $sevenAll;
            $i++;
        }


        //热播剧集
        $hotDrama = DB::table('types')
            -> where('type', '1')
            -> orderBy('id', 'desc')
            -> limit('10')
            -> get();

        //获取中文名
        $hotNames = array();
        $i = 0;
        foreach($hotDrama as $hot)
        {
            $hotName = explode(',', $hot -> name)[0];
            $hotNames[] = $hotName;
            $hotDrama[$i] -> name = $hotNames;
            $i++;
        }

        // 热门电影
        $hotMovie = DB::table('types')
            -> where('type', '0')
            -> orderBy('hot', 'desc')
            -> limit('10')
            -> get();

         //获取中文名
        $hotMovieNames = array();
        $i = 0;
        foreach($hotMovie as $hotMN)
        {
            $hotMovieName = explode(',', $hotMN -> name)[0];
            $hotMovieNames[] = $hotMovieName;
            $hotMovie[$i] -> name = $hotMovieNames;
            $i++;
        }

        //热门电视
        $hotTV = DB::table('types')
            -> where('type', '1')
            -> orderBy('hot', 'desc')
            -> limit(10)
            -> get();

        //获取中文名
        $hotTVNames = array();
        $i = 0;
        foreach($hotTV as $hottv)
        {
            $hotTVName = explode(',', $hottv -> name)[0];
            $hotTVNames[] = $hotTVName;
            $hotTV[$i] -> name = $hotMovieNames;
            $i++;
        }


        
    	return view('home.movie.index', ['firstMovie' => $firstMovie, 'fourMovie' => $fourMovie, 'sevenMovie' => $sevenMovie, 'hotDrama' => $hotDrama, 'hotMovie' => $hotMovie, 'hotTV' => $hotTV]);
    }

    // 电影详情页
    public function details($id)
    {
        $details = DB::table('types') -> where('id', $id) -> first();

        //片名
        $name = explode(',', $details -> name);

        //类型
        $type = explode(',', $details -> pid);

        //编剧
        $scriptwriter = explode(',', $details -> scriptwriter);

        //主演
        $cast = explode('|', $details -> cast);
        $a = array();
        foreach ($cast as $value) 
        {
            $castall = explode(',', $value);
            $a[]=$castall;
        }

        //图片
        $images = DB::table('post')
            -> where('pid', $id)
            -> where('auth', '1')
            -> orderBy('id', 'desc')
            -> limit('4')
            -> get();

        // 增加关注度
        $hotPlus = ($details -> hot) + 1;
        DB::table('types') -> where('id', $id) -> update(['hot' => $hotPlus]);


        // 评论  查询最新的3条评论
        $comment = DB::table('post')
            -> leftJoin('userdetail', 'userdetail.id', '=', 'post.uid')
            -> select('post.*', 'userdetail.photo', 'userdetail.name')
            -> where('pid', $id)
            -> where('auth', '0')
            -> orderBy('post.id', 'desc')
            -> limit('3')
            -> get();

    	return view('home.movie.details', ['details' => $details, 'name' => $name, 'type' => $type, 'scriptwriter' => $scriptwriter, 'cast' => $a, 'images' => $images, 'comment' => $comment]);
    }

    // 视频
    public function trailer(Request $request)
    {
         // 查询单个电影数据
        $mid = $request -> id;
        $oneMovie = DB::table('types') -> where('id', $mid) -> first();
        $oneName = explode(',', $oneMovie -> name);
        $oneMovie -> name = $oneName;


    	return view('home.movie.trailer', ['oneMovie' => $oneMovie]);
    }

    // 图片
    public function image(Request $request)
    {
        // 查询单个电影数据
        $mid = $request -> id;
        $oneMovie = DB::table('types') -> where('id', $mid) -> first();
        $oneName = explode(',', $oneMovie -> name);
        $oneMovie -> name = $oneName;

        //查询电影所有图片
        $images = DB::table('post')
            -> where('pid', $mid)
            -> where('auth', '1')
            -> get();

    	return view('home.movie.image', ['oneMovie' => $oneMovie, 'images' => $images]);
    }

    // 剧情
    public function plots(Request $request)
    {
         // 查询单个电影数据
        $mid = $request -> id;
        $oneMovie = DB::table('types') -> where('id', $mid) -> first();
        $oneName = explode(',', $oneMovie -> name);
        $oneMovie -> name = $oneName;


    	return view('home.movie.plots', ['oneMovie' => $oneMovie]);
    }

    // 其他资料
    public function other(Request $request)
    {
    	 // 查询单个电影数据
        $mid = $request -> id;
        $oneMovie = DB::table('types') -> where('id', $mid) -> first();
        $oneName = explode(',', $oneMovie -> name);
        $oneMovie -> name = $oneName;


        return view('home.movie.other', ['oneMovie' => $oneMovie]);
    }

    // 演员
    public function actor(Request $request)
    {
    	 // 查询单个电影数据
        $mid = $request -> id;
        $oneMovie = DB::table('types') -> where('id', $mid) -> first();
        // 分割电影名称
        $oneName = explode(',', $oneMovie -> name);
        $oneMovie -> name = $oneName;

        // 分割演员名字 英文名 饰演角色名
        $onceCast = explode('|', $oneMovie -> cast);
        $oneCast = array();
        foreach($onceCast as $once)
        {
            $one = explode(',', $once);
            $oneCast[] = $one;
        }

        return view('home.movie.actor', ['oneMovie' => $oneMovie, 'oneCast' => $oneCast]);
    }

    // 评论
    public function comment(Request $request)
    {
    	 // 查询单个电影数据
        $mid = $request -> id;
        $oneMovie = DB::table('types') -> where('id', $mid) -> first();
        $oneName = explode(',', $oneMovie -> name);
        $oneMovie -> name = $oneName;

        // 查询所有评论
        $comment = DB::table('post')
            -> join('userdetail', 'post.uid', '=', 'userdetail.id')
            -> select('post.*', 'userdetail.name', 'userdetail.photo')
            -> where('auth', '0')
            -> where('post.pid', $mid)
            -> orderBy('post.ctime', 'desc')
            -> paginate(4);

        // 查询所有影评回复
        $reply = array();
        foreach($comment as $key => $value)
        {
            $res = DB::table('reply')
                -> join('userdetail', 'reply.uid', '=', 'userdetail.id')
                -> select('userdetail.photo', 'userdetail.name', 'reply.*')
                -> where('pid', $value -> id)
                -> get();
            $reply[$key] = $res;
        }



        

        return view('home.movie.comment', ['oneMovie' => $oneMovie, 'comment' => $comment, 'reply' => $reply]);
    }
}